package com.etalk.crm.serviceImpl;

import com.alibaba.fastjson.JSON;
import com.etalk.crm.dao.PersonMapper;
import com.etalk.crm.dao.StudentInfoMapper;
import com.etalk.crm.pojo.EtMonitoringList;
import com.etalk.crm.pojo.Person;
import com.etalk.crm.pojo.StudentChangeRecord;
import com.etalk.crm.pojo.StudentInfo;
import com.etalk.crm.service.StudentInfoService;
import com.etalk.crm.utils.EncryptAndDecrypt;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: Jordan
 * @date: 2019/1/3 19:12
 * @description:
 */
@Service
public class StudentInfoServiceImpl implements StudentInfoService {
    private static final Logger logger= LogManager.getLogger(StudentInfoServiceImpl.class);

    @Resource
    private PersonMapper personMapper;
    @Resource
    private StudentInfoMapper studentInfoMapper;
    @Resource
    private HttpSession session;

    /**
     * 查询学员信息卡数据
     * @param personId 学员id
     * @return 学员信息
     */
    @Override
    public StudentInfo searchStudentInfo(Integer personId){
        StudentInfo result = personMapper.selectStudentInfoByPersonId(personId);
        //生成tokenString
        Map<String, Object> datamap=new HashMap<>(6);
        datamap.put("loginId", result.getPersonId());
        datamap.put("loginName", result.getLoginName());
        datamap.put("roleId", 3);
        datamap.put("storeId",result.getStoresId());
        result.setTokenString(EncryptAndDecrypt.encrypt(JSON.toJSONString(datamap)));
        List<Map<String,Object>> weixinList=personMapper.selectWeixinByPersonId(personId);
        result.setBindingWeixin(weixinList.size()>0);
        result.setListWeixin(weixinList);
        List<Map<String,Object>> appList=personMapper.selectAppOnlineByPersonId(personId);
        result.setBindingApp(appList.size()>0);
        result.setListApp(appList);
        return result;
    }

    /**
     * 查询学员信息变更记录
     * @param personId 学员id
     * @param pageNum 页码
     * @param pageSize 每页记录
     * @return 数据对象
     */
    @Override
    public PageInfo<StudentChangeRecord> searchStudentChangRecordByPage(Integer personId,int pageNum,int pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<StudentChangeRecord> list=personMapper.selectStudentChangeRecord(personId);
        return new PageInfo<>(list);
    }

    /**
     * 查询et问题监控用户列表
     * @param searchKey 查询关键字
     * @param pageNum 当前页
     * @param pageSize 每页记录数
     * @return 列表
     */
    @Override
    public PageInfo<EtMonitoringList> searchEtMonitoringListByPage(String searchKey,int pageNum,int pageSize){

        PageHelper.startPage(pageNum,pageSize);
        List<EtMonitoringList> list=studentInfoMapper.selectMonitorList(searchKey);
        return new PageInfo<>(list);
    }

    /**
     * 添加问题用户记录
     * @param personId 用户id
     * @param content 问题内容
     * @return 记录数
     */
    @Override
    public int addEtMonitorPerson(int personId,String content){
        int num=0;
        try {
            num = studentInfoMapper.insetMonitorInfo(personId,content,session.getAttribute("loginname").toString());
        }catch (Exception e){
            if(e instanceof DuplicateKeyException) {
                num = -1;
            }
            logger.error("添加问题用户监控出错："+e.getMessage(),e);
        }
        return num;
    }

    /**
     * 修改监控内容
     * @param personId 用户id
     * @param content 监控内容
     * @return 记录数
     */
    @Override
    public int updateEtMonitorPersonByContent(int personId,String content){
        return studentInfoMapper.updateMonitorContentByPersonId(personId,content,session.getAttribute("loginname").toString());
    }

    /**
     * 修改监控状态
     * @param personId 用户id
     * @param state 是否启用监控：0否，1是
     * @return 记录数
     */
    @Override
    public int updateEtMonitorPersonByState(int personId,int state){
        return studentInfoMapper.updateMonitorStateByPersonId(personId,state,session.getAttribute("loginname").toString());
    }

    /**
     * 删除监控记录
     * @param personId 用户id
     * @return 记录数
     */
    @Override
    public int delEtMonitorInfoByPersonId(int personId){
        return studentInfoMapper.deleteMonitorRecordByPersonId(personId);
    }

    /**
     * 查询学员老师信息
     * @param loginName 查询关键字
     * @return 用户信息
     */
    @Override
    public List<Person> searchPersonListBySearchKey(String loginName) {
        return studentInfoMapper.selectPersonList(loginName);
    }
}
